package com.dailyblue.project.estate.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.commons.bean.Activity;
import com.dailyblue.project.estate.commons.handler.ActivityException;
import com.dailyblue.project.estate.commons.mapper.ActivityMapper;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import com.dailyblue.project.estate.service.ActivityService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;

/**
 * @Auther:Liu
 * @Testname:ActivityServiceImpl
 * @Date:2025/9/18 11:51
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Resource
    private ActivityMapper activityMapper;
    @Override
    public JsonResult saveActivity(Activity activity) {
        boolean b = save(activity);
        if (!b){
            throw new ActivityException("添加活动失败");
        }
        return ResultTool.success("save activity");
    }

    @Override
    public JsonResult findOneById(String id) {
        Activity activity = getById(id);
        return ResultTool.success(activity);
    }

    @Cacheable(cacheNames = "ActivityAll")
    public JsonResult findAllActivity() {
        List<Activity> list = list();
        return ResultTool.success(list);
    }

    @CacheEvict(cacheNames = "ActivityAll",allEntries = true)
    public JsonResult deleteActivity(String id) {
        boolean b = removeById(id);
        if (!b){
            throw new ActivityException("删除活动失败");
        }
        return ResultTool.success("删除活动成功");
    }

    @CacheEvict(cacheNames = "ActivityAll",allEntries = true)
    public JsonResult updateActivity(Activity activity) {
        UpdateWrapper<Activity> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", activity.getId());
        wrapper.set("community_id", activity.getCommunityId());
        wrapper.set("community_name", activity.getCommunityName());
        wrapper.set("title", activity.getTitle());
        wrapper.set("address", activity.getAddress());
        wrapper.set("organizer", activity.getOrganizer());
        wrapper.set("status", activity.getStatus());
        int update = activityMapper.update(wrapper);
        if (update>=0){
            return ResultTool.success("更新活动成功");
        }
        throw new ActivityException("更新活动失败");
    }
}
